//
//  BaseScrollView.m
//  ZITHER
//
//  Created by weixin on 2018/10/15.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "BaseScrollView.h"

@implementation BaseScrollView

-(instancetype)initializeFrame:(CGRect)frame contentSize:(CGSize)contentSize{
    [self init].frame = frame;
    self.backgroundColor = [UIColor randomColor];
    self.bounces = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.bounces = NO; // 默认为YES 取消设置NO
    // 当bounces属性为NO时，以下两个属性设置无效
    self.alwaysBounceHorizontal = YES; // 水平方向弹簧效果
    self.alwaysBounceVertical = YES; // 垂直方向弹簧效果
    self.decelerationRate = 0.1; // 值域 0~1 值越小减速停止的时间越短
    self.pagingEnabled = YES;
    self.contentSize = contentSize;
    
    return self;
}
-(void)addScrImage:(NSArray *)arr imageHeight:(CGFloat)imgH Radius:(CGFloat)radius{
    if (arr.count == 0 || arr == nil) {
        self.hidden = YES;
        return;
    }
    self.arrMImage1 = arr;
    //    self.backgroundColor = [UIColor redColor];
    self.hidden = NO;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.contentSize = CGSizeMake(arr.count*(imgH+10),0);
    if (arr.count*imgH < kScreenW-90) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(arr.count*imgH);
        }];
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenW-90);
        }];
    }
    self.userInteractionEnabled = YES;
    
    for (int j = 0; j<arr.count; j++) {
        BaseImageV * imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(j*(imgH+10),0, imgH,imgH) contentMode:2];
        imageV.clipsToBounds = YES;
        [imageV setCornerRadiusWithRadius:radius isMasks:YES];
        NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,arr[j]];
        [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"watchSmale"]];
        imageV.userInteractionEnabled = NO;
        imageV.tag = j +100;
        [self addSubview:imageV];
        [[imageV addTag]subscribeNext:^(id  _Nullable x) {
            [self showBigImage:imageV.tag];
        }];
    }
}




-(void)addScrImage:(NSArray *)arr imageHeight:(CGFloat)imgH{
    if (arr.count == 0 || arr == nil) {
        self.hidden = YES;
        return;
    }
    self.arrMImage1 = arr;
    //    self.backgroundColor = [UIColor redColor];
    self.hidden = NO;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.contentSize = CGSizeMake(arr.count*(imgH+10),0);
    if (arr.count*imgH < kScreenW-90) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(arr.count*imgH);
        }];
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenW-90);
        }];
    }
    self.userInteractionEnabled = YES;
    
    for (int j = 0; j<arr.count; j++) {
        BaseImageV * imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(j*(imgH+10),0, imgH,imgH) contentMode:2];
        
        imageV.clipsToBounds = YES;
        //        BaseImageV * imageV = [[BaseImageV alloc]initWithFrame:CGRectMake(j*68,0, imgH,imgH)];
        //        [imageV setCornerRadiusWithRadius:5 isMasks:YES];
        //        imageV.backgroundColor = [UIColor randomColor];
        //        imageV.contentMode = UIViewContentModeScaleAspectFit;
        NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,arr[j]];
        [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"watchSmale"]];
        imageV.userInteractionEnabled = NO;
        imageV.tag = j +100;
        [self addSubview:imageV];
        [[imageV addTag]subscribeNext:^(id  _Nullable x) {
            [self showBigImage:imageV.tag];
        }];
    }
}

-(void)addScrImage:(NSArray *)arr{
    
    if (arr.count == 0 || arr == nil) {
        self.hidden = YES;
        return;
    }
    
    self.hidden = NO;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.contentSize = CGSizeMake(arr.count*80,0);
    
    //    if (arr.count*80 < kScreenW-40) {
    //        self.userInteractionEnabled = NO;
    //    }else{
    self.userInteractionEnabled = YES;
    //    }
    
    for (int j = 0; j<arr.count; j++) {
        BaseImageV * imageV = [[BaseImageV alloc]initWithFrame:CGRectMake(j*68,0, 80,80)];
        //        [imageV setCornerRadiusWithRadius:5 isMasks:YES];
        imageV.backgroundColor = [UIColor randomColor];
        imageV.contentMode = UIViewContentModeScaleAspectFit;
        NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,arr[j]];
        [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"watchSmale"]];
        imageV.userInteractionEnabled = YES;
        imageV.tag = j +100;
        [self addSubview:imageV];
        [[imageV addTag]subscribeNext:^(id  _Nullable x) {
            [self showBigImage:imageV.tag];
        }];
    }
    
}



-(void)showBigImage:(NSInteger)selectIndex{
    NSLog(@"107w行 输出点击selectIndex%ld",selectIndex);

    UIImageView *imageV = [self viewWithTag:selectIndex];
    // self.model.fileList[selectIndex-100];
    
    
    if (!self.imagePreviewViewController) {
        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        self.imagePreviewViewController.imagePreviewView.delegate = self;
        
        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
        
    }
    
    __weak __typeof(self)weakSelf = self;
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        NSLog(@"124行 输出aImagePreviewViewController.imagePreviewView.currentImageIndex%ld",aImagePreviewViewController.imagePreviewView.currentImageIndex);

        NSInteger index = aImagePreviewViewController.imagePreviewView.currentImageIndex;
        UIImageView *imageV1 = [weakSelf viewWithTag:index+100];
        NSLog(@"124行 输出index+100%ld",index+100);

        //        imageV.image = currentZoomImageView.image;
        //            [weakSelf.imageButton setImage:currentZoomImageView.image forState:UIControlStateNormal];
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:[self convertRect:imageV1.frame toView:nil]];
        
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  selectIndex-100;// 默认查看的图片的 index
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[self convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
}




#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    NSLog(@"142 行 输出self.arrMImage.conut = %ld",self.arrMImage1.count);
    return self.arrMImage1.count;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    
    
    NSLog(@"154行 输出index+100%ld",index+100);

    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
        [zoomImageView hideEmptyView];
        
        UIImageView *imageV = [self viewWithTag:index+100];
        zoomImageView.image = imageV.image;
        
    }
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //
    //    });
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
}

@end
